# -*- coding: utf-8 -*-
"""
Created on Sun Jan  3 22:30:22 2016

@author: liuyi05
"""

import os
import multiprocessing.dummy
import tushare as ts
import pandas as pd

outdir = "data/latest/"
#header = ["open", "high", "low", "close", "volume", "p_change", "turnover"]
# "['turnover'] not in index"
header = ["open", "high", "low", "close", "volume", "p_change"]
period = pd.DateOffset(years=1)

def handle_code(code):
    path = outdir + code + ".asc"
    if not os.path.exists(path):
        df = ts.get_hist_data(code)[header]
    else:
        df = pd.read_csv(path, index_col="date")
        begin = (pd.to_datetime(df.index[-1]) + pd.DateOffset(days=1)) \
                .strftime("%Y-%m-%d")
        
        hist_data = ts.get_hist_data(code, start=begin)
        if hist_data is None:
            print("skip None type data with code: %s" % code)
            return
        other = hist_data[header]
        if other.empty:
            print("code=[%s] has no more data" % code)
            return
        df = df.append(other)
    df.sort_index(inplace=True)
    oldest = (pd.to_datetime(df.index[-1]) - period).strftime("%Y-%m-%d")
    df[df.index > oldest].to_csv(path)

def main():
    code_list_path = "data/code.list"
    with open(code_list_path) as file:
        code_list = [line.rstrip() for line in file.readlines()]
        proc = multiprocessing.dummy.Pool(10)
        proc.map(handle_code, code_list)
        #handle_code(code_list[0])
            
if __name__ == "__main__":
    main()